package cn.itcast.usermanager.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;

import cn.itcast.usermanager.mapper.NewUserMapper;
import cn.itcast.usermanager.pojo.User;
import cn.itcast.usermanager.service.UserService;

@Service
public class NewUserServiceImpl implements UserService {

	// 注入通用Mapper:UserMapper
	@Autowired
	private NewUserMapper newUserMapper;

	/**
	 * 分页查询用户
	 */
	public List<User> queryUserByPage(Integer pageNum, Integer pageSize) {
		// 开启分页
		PageHelper.startPage(pageNum, pageSize);
		// 调用mapper查询
		List<User> list = newUserMapper.select(null);
		PageInfo<User> userPageInfo = new PageInfo<>(list);
		List<User> list1 = userPageInfo.getList();
		long total = userPageInfo.getTotal();
		System.out.println(list1);
		System.out.println(total);

		return list;
	}

	/**
	 * 添加用户
	 */
	public Integer addUser(User user) {
		return newUserMapper.insertSelective(user);
	}

	/**
	 * 修改用户
	 */
	public Integer updateUser(User user) {
		return newUserMapper.updateByPrimaryKeySelective(user);
	}

	/**
	 * 删除用户
	 */
	public Integer deleteUserByIds(String[] ids) {
		List<Object> list = new ArrayList<Object>();
		for (String id : ids) {
			list.add(id);
		}
		Example example = new Example(User.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id", list);
		return newUserMapper.deleteByExample(example);
	}

	/**
	 * 根据id查询用户
	 */
	public User queryUserById(Long id) {
		return newUserMapper.selectByPrimaryKey(id);
	}

}
